home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / comm / mail / YAMscripts.lha / WholeStory.rexx < prev    next >
OS/2 REXX Batch file  |  1997-06-24  |  5KB  |  202 lines

  1. /*
  2. ** WholeStory.rexx 1.4 - 15-Mar-96 by Kai.Nikulainen@utu.fi
  3. **
  4. ** I belong to a X-Files fanfic mailing list and long stories are usually posted
  5. ** in parts.  This script will find and join all parts.
  6. **
  7. ** New in 1.4
  8. **  -creates a better filename
  9. **  -returns to the correct message, even when aborted
  10. **  -one bug removed which caused sometimes an error                                                                                                                                            xs
  11. ** New in 1.3:
  12. **  -now hadles both (n/m) and n/m numbering correctly
  13. ** New in 1.2:
  14. **  -script now displays a requester with message subjects 
  15. **  -messages are marked deleted AFTER they have been copied to the file
  16. **  -an end message tells you when everything is done
  17. ** Send all comments, bug reports, suggestions and X-mas cards to knikulai@utu.fi
  18. */
  19.  
  20. options results
  21. call addlib('rexxsupport.library',0,-30)
  22. call addlib('rexxreqtools.library',0,-30)
  23.  
  24.  defdir='work:home'
  25.   qbody='How similar the subjects need to be?'
  26.   qbuts='_Very|_Quite|_Cancel'
  27.    body='I found these parts in this order:'
  28. buttons='_Save them|_Quit'
  29.   title='WholeStory 1.2 © knikulai@utu.fi'
  30.    tags='rt_pubscrname=YAMSCREEN'  /* Change here the name of the screen YAM runs */
  31. mc=0 
  32. used=0
  33. NL='0a'x
  34.  
  35. address 'YAM'
  36. 'GetMailInfo From'
  37. server=result
  38. 'GetMailInfo Subject'
  39. subj=result
  40. 'GetMailInfo Active'
  41. active=result
  42. 'GetFolderInfo Max'
  43. n=result
  44. maxdif=rtezrequest(qbody,qbuts,title,'rtez_defaultresponse=1' tags)*2-1
  45. if maxdif=0 then exit
  46. /* 
  47. ** First scan all messages in the folder.
  48. ** Search for messages with same sender and at most 4 different chars in subject 
  49. */
  50.  
  51. do m=0 to n-1
  52.     'SetMail' m
  53.     'GetMailInfo From'
  54.     if result=server then do
  55.        'GetMailInfo Subject'
  56.        if SubDiff(upper(result),upper(subj))<=maxdif then do
  57.                call AddMsg(result)
  58.         part.mc=m
  59.         end
  60.     end /* if result=server then do */
  61. end /* do m=1 to n */
  62. 'SetMail' active  /* Go back to the originally selected message */
  63.  
  64. /*
  65. ** Then sort the messages.  Hopefully part 10 comes after 9 and not after 1....
  66. */
  67.  
  68. call SortMessages
  69.  
  70. /*
  71. ** Do you really want to save them?
  72. */
  73. do i=1 to mc
  74.     body=body NL subject.i
  75.     end
  76.  
  77. sel=rtezrequest(body,buttons,title,'rtez_defaultresponse=1' tags)
  78. if sel=0 then exit
  79.  
  80. /*
  81. ** Lets write the messages into one file
  82. */
  83. storyname=compress(subject.1,',/:()*<>[]"0123456789')
  84. if upper(left(storyname,3))='NEW' then storyname=substr(storyname,4)
  85. if upper(left(storyname,6))='REPOST' then storyname=substr(storyname,7)
  86. storyname=strip(left(storyname,25,''))
  87. storyname=translate(storyname,'_',' ')
  88. outfile=rtfilerequest(defdir,storyname,'Select story name' tags)
  89. if outfile='' then exit
  90.  
  91. call open(out,outfile,'w')
  92. call writeln(out,'This file contains the following messages:')
  93. do i=1 to mc
  94.         call writeln(out,subject.i)
  95.         end /* do i=1 to mc */
  96. call writeln(out,'')
  97. address 'YAM' 
  98. do i=1 to mc
  99.         if ~open(inp,filename.i,'r') then do
  100.         'Request "Can not read part*n'subject.i'" "Ok"'
  101.         'SetMail' active  /* Go back to the originally selected message */ 
  102.         exit
  103.         end
  104.     do until eof(inp) | r='' /* Skip headers */
  105.         r=readln(inp)
  106.         end
  107.     do until eof(inp)
  108.         r=readln(inp)
  109.         call writeln(out,r)
  110.         end
  111.     call close(inp)
  112.     'SetMail' part.i
  113.     'MailDelete'
  114.         end /* do i=1 to mc */
  115. call close(out)
  116. 'Request "All parts are saved and marked as deleted!" "_Ok"'
  117.  
  118. exit    /* It's the end of the script as we know it... */
  119.  
  120. FindStart:
  121.   found_it=0
  122.   if used=0 then do
  123.   /* 
  124.   ** When called the first time, this branch checks which delimiters are used 
  125.   ** Following calls use else branch
  126.   */
  127.      do until eof(1) | found_it
  128.          rivi=readln(1)
  129.          do d=1 to delimiters
  130.              if pos(startline.d,rivi)=1 then do
  131.               found_it=1
  132.               used=d
  133.           end /* if */
  134.               end /* do d=1 */
  135.          end /*do until */
  136.      if used=3 then call writeln(tmp,rivi)  /* Save uuencode first line */
  137.      end /* ifused=0 */
  138.   else do
  139.      do until eof(1) | found_it
  140.          rivi=readln(1)
  141.          found_it=pos(startline.used,rivi)
  142.          end /*do until */  
  143.      end /* else do */
  144. return found_it
  145.  
  146. SubDiff: procedure
  147.   /* Differences between strings are calculate by word */
  148.   parse arg s1,s2
  149.   diff=0
  150.   do i=1 to words(s1)
  151.     diff=diff+ (word(s1,i)~=word(s2,i))
  152.   end
  153. return diff
  154.  
  155.  
  156. AddMsg:
  157. /*
  158. ** This changes the subject's (1/n) to (01/n) to allow sorting
  159. */
  160.   parse arg s
  161.   mc=mc+1
  162.   bra=lastpos('(',s)
  163.   if bra>0 then do
  164.       slash=pos('/',s,bra)
  165.       if slash=0 then do
  166.           slash=lastpos('/',s)
  167.           if slash>0 then bra=lastpos(' ',s,slash)
  168.       end
  169.       end
  170.   else do
  171.       slash=lastpos('/',s)
  172.       if slash>0 then bra=lastpos(' ',s,slash)
  173.       end
  174.   if slash-bra=2 then s=left(s,bra)'00'substr(s,bra+1)
  175.   if slash-bra=3 then s=left(s,bra)'0'substr(s,bra+1)
  176.   subject.mc=s
  177.   'GetMailInfo File'
  178.   filename.mc=result
  179. return
  180.  
  181. SortMessages:
  182. /* 
  183. ** A simple algorithm is fastest with relatively few items.
  184. ** There should be no need for something fancy like quicksort :-) 
  185. */
  186.   do i=2 to mc
  187.      do j=1 to i-1
  188.         if upper(subject.j)>upper(subject.i) then do/* let's swap stuff... */
  189.         temp=subject.j
  190.         subject.j=subject.i
  191.         subject.i=temp
  192.         temp=filename.j
  193.         filename.j=filename.i
  194.         filename.i=temp
  195.         temp=part.j
  196.         part.j=part.i
  197.         part.i=temp
  198.         end /* if */
  199.      end /* do j */
  200.   end /* do i */
  201. return  /* Everything should be in order now... */
  202.